<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>第三方登陆 &middot; 宋申奥的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/hugo_blog/"><h1>宋申奥的博客</h1></a>
      <p class="lead">
       这里是宋申奥的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/hugo_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>第三方登陆</h1>
  <time datetime=2020-04-28T17:07:35Z class="post-date">Tue, Apr 28, 2020</time>
  <h1 id="第三方登录">第三方登录</h1>
<hr>
<h2 id="思维引导图">思维引导图</h2>
<p><img src="/bike/wen_img/18.png" alt="aa"></p>
<hr>
<h2 id="微博登录范例">微博登录范例</h2>
<h3 id="vue登陆页面请求微博登陆接口">vue登陆页面请求微博登陆接口</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue">&lt;<span style="color:#f92672">template</span>&gt;
    &lt;<span style="color:#f92672">div</span>&gt;
        &lt;<span style="color:#f92672">button</span> <span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">click</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;sina&#39;</span>&gt;<span style="color:#a6e22e">微博登陆</span>&lt;/<span style="color:#f92672">button</span>&gt;
    &lt;/<span style="color:#f92672">div</span>&gt;
&lt;/<span style="color:#f92672">template</span>&gt;
&lt;<span style="color:#f92672">script</span>&gt;
<span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> {
    <span style="color:#a6e22e">methods</span><span style="color:#f92672">:</span>{
        <span style="color:#a6e22e">sina</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>() { 
            <span style="color:#75715e">//跳转 
</span><span style="color:#75715e"></span>            <span style="color:#75715e">//微博api：https://api.weibo.com/oauth2/authorize
</span><span style="color:#75715e"></span>            <span style="color:#75715e">//client_id：微博开放平台的appid
</span><span style="color:#75715e"></span>            <span style="color:#75715e">//redirect_uri：返回的跳转地址
</span><span style="color:#75715e"></span>            window.<span style="color:#a6e22e">location</span>.<span style="color:#a6e22e">href</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;https://api.weibo.com/oauth2/authorize?client_id=873314913&amp;redirect_uri=http://127.0.0.1:8000/md_admin/weibo&#34;</span>;
            },
    }
}
&lt;/<span style="color:#f92672">script</span>&gt;
</code></pre></div><h3 id="django路由">django路由</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> myapp.md_user <span style="color:#f92672">import</span> wb_back
urlpatterns <span style="color:#f92672">=</span> [
    path(<span style="color:#e6db74">&#39;md_admin/weibo/&#39;</span>,wb_back),
]
</code></pre></div><h3 id="django接口">django接口</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">wb_back</span>(request):
    <span style="color:#75715e">#接收参数</span>
    code <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;code&#39;</span>,None)
    <span style="color:#75715e">#定义token接口地址</span>
    url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://api.weibo.com/oauth2/access_token&#39;</span>
    <span style="color:#75715e">#定义参数</span>
    re <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>post(url,data<span style="color:#f92672">=</span>{
        <span style="color:#e6db74">&#34;client_id&#34;</span>:<span style="color:#e6db74">&#34;873314913&#34;</span>,
        <span style="color:#e6db74">&#34;client_secret&#34;</span>:<span style="color:#e6db74">&#34;01f43512a74bfde158d5b8027726a50a&#34;</span>,
        <span style="color:#e6db74">&#34;grant_type&#34;</span>:<span style="color:#e6db74">&#34;authorization_code&#34;</span>,
        <span style="color:#e6db74">&#34;code&#34;</span>:code,
        <span style="color:#e6db74">&#34;redirect_uri&#34;</span>:<span style="color:#e6db74">&#34;http://127.0.0.1:8000/md_admin/weibo&#34;</span>
    })
    <span style="color:#75715e">#换取微薄昵称</span>
    res <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;https://api.weibo.com/2/users/show.json&#39;</span>,params<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#39;access_token&#39;</span>:re<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;access_token&#39;</span>],<span style="color:#e6db74">&#39;uid&#39;</span>:re<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;uid&#39;</span>]})

    sina_id <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
    user_id <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
    <span style="color:#75715e">#判断是否登陆过</span>
    user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(username<span style="color:#f92672">=</span>str(res<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;name&#39;</span>]))<span style="color:#f92672">.</span>first()
    <span style="color:#66d9ef">if</span> user:
        <span style="color:#75715e">#登陆过就取出id和username</span>
        sina_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>username
        user_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>id
    <span style="color:#66d9ef">else</span>:
        <span style="color:#75715e">#没登陆过就将微博名写成username把微博id写成md5加密的密码存入数据库</span>
        user <span style="color:#f92672">=</span> User(username<span style="color:#f92672">=</span>str(res<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;name&#39;</span>]),password<span style="color:#f92672">=</span>make_password(str(res<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;id&#39;</span>])))
        user<span style="color:#f92672">.</span>save()
        <span style="color:#75715e">#因为微博昵称可能会变而微博id不变，所以用password当作查询条件，取出username和id</span>
        user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(password<span style="color:#f92672">=</span>make_password(str(res<span style="color:#f92672">.</span>json()[<span style="color:#e6db74">&#39;id&#39;</span>])))<span style="color:#f92672">.</span>first()
        sina_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>username
        user_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>id
    <span style="color:#75715e">#将username和id发送至前端</span>
    <span style="color:#66d9ef">return</span> redirect(<span style="color:#e6db74">&#39;http://127.0.0.1:8080?sina_id=&#39;</span><span style="color:#f92672">+</span>str(sina_id)<span style="color:#f92672">+</span><span style="color:#e6db74">&#34;&amp;uid=&#34;</span><span style="color:#f92672">+</span>str(user_id))
</code></pre></div><h3 id="vue第三方登陆的接入">vue第三方登陆的接入</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue">&lt;<span style="color:#f92672">script</span>&gt;
<span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> {
  <span style="color:#a6e22e">mounted</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(){
        <span style="color:#75715e">//接受第三方参数
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">sina_id</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">$route</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">sina_id</span>;
        <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">user_id</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">$route</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">uid</span>;
        <span style="color:#75715e">//判断是否有第三方登陆
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">sina_id</span>){
            <span style="color:#75715e">//存入localStorage
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">localStorage</span>.<span style="color:#a6e22e">setItem</span>(<span style="color:#e6db74">&#39;username&#39;</span>,<span style="color:#a6e22e">sina_id</span>)
            <span style="color:#a6e22e">localStorage</span>.<span style="color:#a6e22e">setItem</span>(<span style="color:#e6db74">&#39;uid&#39;</span>,<span style="color:#a6e22e">user_id</span>)
        }
    }
}
&lt;/<span style="color:#f92672">script</span>&gt;
</code></pre></div><hr>
<h2 id="钉钉第三方扫码登陆">钉钉第三方扫码登陆</h2>
<h3 id="vue钉钉扫码接口">vue钉钉扫码接口</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue"><span style="color:#75715e">//钉钉登陆
</span><span style="color:#75715e"></span><span style="color:#a6e22e">dingding</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>() { 
    <span style="color:#75715e">//跳转
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//appid为钉钉开放平台应用的appid
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//redirect_uri为回调地址
</span><span style="color:#75715e"></span>    window.<span style="color:#a6e22e">location</span>.<span style="color:#a6e22e">href</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://oapi.dingtalk.com/connect/qrconnect?appid=dingoagvulsdkmz3tmutfp&amp;response_type=code&amp;scope=snsapi_login&amp;state=STATE&amp;redirect_uri=http://127.0.0.1:8000/md_admin/dingding&#39;</span>
    },
</code></pre></div><h3 id="django接口-1">django接口</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> base64
<span style="color:#f92672">import</span> hmac
<span style="color:#f92672">import</span> urllib

<span style="color:#75715e"># 钉钉回调</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">ding_back</span>(request):
    code <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;code&#39;</span>)
    t <span style="color:#f92672">=</span> time<span style="color:#f92672">.</span>time()
    <span style="color:#75715e"># 时间戳  appSecret为钉钉开放平台应用的appsecret</span>
    timestamp <span style="color:#f92672">=</span> str((int(round(t <span style="color:#f92672">*</span> <span style="color:#ae81ff">1000</span>))))
    appSecret <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Neijy8PB_rEoaSKcUHhwz-ddj44_Y_Zt-xJ_RbiXMIy2VMVl6erF_B6ceXvakq0N&#39;</span>
    <span style="color:#75715e"># 构造签名</span>
    signature <span style="color:#f92672">=</span> base64<span style="color:#f92672">.</span>b64encode(
        hmac<span style="color:#f92672">.</span>new(appSecret<span style="color:#f92672">.</span>encode(<span style="color:#e6db74">&#39;utf-8&#39;</span>), timestamp<span style="color:#f92672">.</span>encode(<span style="color:#e6db74">&#39;utf-8&#39;</span>), digestmod<span style="color:#f92672">=</span>hashlib<span style="color:#f92672">.</span>sha256)<span style="color:#f92672">.</span>digest())
    <span style="color:#75715e"># 请求接口，换取钉钉用户名  accessKey为钉钉开放平台应用的appid</span>
    payload <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;tmp_auth_code&#39;</span>: code}
    headers <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Content-Type&#39;</span>: <span style="color:#e6db74">&#39;application/json&#39;</span>}
    res <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>post(<span style="color:#e6db74">&#39;https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature=&#39;</span> <span style="color:#f92672">+</span> urllib<span style="color:#f92672">.</span>parse<span style="color:#f92672">.</span>quote(signature<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#34;utf-8&#34;</span>)) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;&amp;timestamp=&#34;</span> <span style="color:#f92672">+</span> timestamp <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;&amp;accessKey=dingoagvulsdkmz3tmutfp&#34;</span>,data<span style="color:#f92672">=</span>json<span style="color:#f92672">.</span>dumps(payload), headers<span style="color:#f92672">=</span>headers)
    res_dict <span style="color:#f92672">=</span> json<span style="color:#f92672">.</span>loads(res<span style="color:#f92672">.</span>text)
 
    user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(username<span style="color:#f92672">=</span>str(res_dict[<span style="color:#e6db74">&#39;user_info&#39;</span>][<span style="color:#e6db74">&#39;nick&#39;</span>]))<span style="color:#f92672">.</span>first()

    username <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
    user_id <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
    <span style="color:#66d9ef">if</span> user:
        username <span style="color:#f92672">=</span> res_dict[<span style="color:#e6db74">&#39;user_info&#39;</span>][<span style="color:#e6db74">&#39;nick&#39;</span>]
        user_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>id
    <span style="color:#66d9ef">else</span>:
        User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>create(username<span style="color:#f92672">=</span>str(res_dict[<span style="color:#e6db74">&#39;user_info&#39;</span>][<span style="color:#e6db74">&#39;nick&#39;</span>]), password<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;&#39;</span>)
        user <span style="color:#f92672">=</span> User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(username<span style="color:#f92672">=</span>str(res_dict[<span style="color:#e6db74">&#39;user_info&#39;</span>][<span style="color:#e6db74">&#39;nick&#39;</span>]))<span style="color:#f92672">.</span>first()
        username <span style="color:#f92672">=</span> res_dict[<span style="color:#e6db74">&#39;user_info&#39;</span>][<span style="color:#e6db74">&#39;nick&#39;</span>]
        user_id <span style="color:#f92672">=</span> user<span style="color:#f92672">.</span>id

    <span style="color:#66d9ef">return</span> redirect(<span style="color:#e6db74">&#39;http://localhost:8080/?username=&#39;</span> <span style="color:#f92672">+</span> str(username) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;&amp;&#39;</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;user_id=&#39;</span> <span style="color:#f92672">+</span> str(user_id))

</code></pre></div>
</div>


    </main>

    
  </body>
</html>
